package com.shu.serializationTest;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.IOException;

/**
 * @author ssm
 * @version V1.0
 * @description:
 * @date 2019/4/10 12:52
 */

public class SerializationTest1 {
    User user;

    @BeforeEach
    void init() {
        user = new User();
        user.setGoodman(true);
        user.setHandsom(true);
        user.setName("哈哈大鳄");
    }

    @Test
    @DisplayName("boolean类型属性命名和反序列化 - fastjson")
    void testFastjsonSerialize() {
        String serialObj = String.valueOf(JSON.toJSON(user));
        System.out.println("序列化结果:" + serialObj);

        User deserializeObj = JSON.parseObject(serialObj, User.class);
        System.out.println("反序列化结果:" + deserializeObj.toString());
    }

    @Test
    @DisplayName("boolean类型属性命名和反序列化 - Jackson")
    void testJacksonSerialize() throws IOException {
        ObjectMapper mapper = new ObjectMapper();

        String serialObj = String.valueOf(mapper.writeValueAsString(user));
        System.out.println("序列化结果:" + serialObj);

        User deserializeObj = mapper.readValue(serialObj, User.class);
        System.out.println("反序列化结果:" + deserializeObj.toString());

    }
}


class User {
    // 某些框架在反向解析的时候，“误以为”对应的属性名称是 handsom，导致属性获取不到，进而抛 出异常。
    private boolean isHandsom;
    private boolean goodman;
    private String name;

    public boolean isHandsom() {
        return isHandsom;
    }

    public void setHandsom(boolean handsom) {
        isHandsom = handsom;
    }

    public boolean isGoodman() {
        return goodman;
    }

    public void setGoodman(boolean goodman) {
        this.goodman = goodman;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                   "isHandsom=" + isHandsom +
                   ", goodman=" + goodman +
                   ", name='" + name + '\'' +
                   '}';
    }
}
